package peng.yang.express.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;

import peng.yang.express.Adapter.ImageViewPagerGuidAdapter;
import peng.yang.express.Interface.OnPagerClickListener;
import peng.yang.express.R;

/**
 * 功能描述：引导界面activity类
 */

public class ViewPagerGuid extends AppCompatActivity implements OnPagerClickListener{
    // 定义ViewPager对象
    private ViewPager viewPager;

    // 定义ViewPager适配器
    private ImageViewPagerGuidAdapter vpAdapter;

    // 定义一个ArrayList来存放View
    private ArrayList<View> views;

    // 定义各个界面View对象
    private View view1, view2, view3, view4;

    //定义开始按钮对象
    private Button btstart;
    private final static String DEFAULT_FILE_NAME = "file";

    private OnPagerClickListener pagerClickListener;
    private boolean isFirst1;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /**
         * 判断是否第一次登录
         */
        sharedPreferences = getSharedPreferences(DEFAULT_FILE_NAME, MODE_PRIVATE);
        isFirst1 = sharedPreferences.getBoolean("isFirst",true);
        if(isFirst1){
            SharedPreferences.Editor eidt=  sharedPreferences.edit();
            eidt.putBoolean("isFirst",false);
            eidt.commit();

            setContentView(R.layout.activity_view_pager_guid);
            findView();
            initData();
        }else {
            startActivity(new Intent(ViewPagerGuid.this,MainActivity.class));
            this.finish();
        }

    }




    /**
     * 初始化组件
     */
    private void findView(){

        //实例化各个界面的布局对象
        LayoutInflater mLi = LayoutInflater.from(this);
        view1 = mLi.inflate(R.layout.guid_view01, null);
        view2 = mLi.inflate(R.layout.guid_view02, null);
        view3 = mLi.inflate(R.layout.guid_view03, null);
        view4 = mLi.inflate(R.layout.guid_view04, null);

        // 实例化ViewPager
        viewPager = (ViewPager) findViewById(R.id.vp_guid);

        // 实例化ArrayList对象
        views = new ArrayList<View>();

        // 实例化ViewPager适配器
        vpAdapter = new ImageViewPagerGuidAdapter(this, views, pagerClickListener);

        btstart = (Button)view4.findViewById(R.id.bt_guid);

    }
    /**
     * 初始化数据
     */
    private void initData(){

        //将要分页显示的View装入数组中
        views.add(view1);
        views.add(view2);
        views.add(view3);
        views.add(view4);
        vpAdapter.notifyDataSetChanged();

        // 设置适配器数据
        viewPager.setAdapter(vpAdapter);

        // 给开始按钮设置监听
        btstart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ViewPagerGuid.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        });

    }

    @Override
    public void onPageClick(Context context, int position) {

    }
}
